from views import page as pageBase

from api import api


class TestUser(object):
    page = pageBase.resUsers

    def login(self, user, password):
        self.page.login(user, password)
        # self.page.setCompany()

    # def setCompany(self):
    #     self.page.setCompany()

    def test(self,):
        self.test_login()

        # self.test_company()

        # self.test_create()
        # self.test_create_company()
        # self.test_user_company()

        # self.test_search()
        # print(res)
    def test_login(self):

        res = api.rpc.session_check()
        print(res)
        # res = api.rpc.get_session_info()
        # print(res)

    def test_company(self):

        # Ptn = api.odooenv['product.template']
        # pid = Ptn.search_read([], ['name', 'company_id'])
        # print('ptn ok,', pid,)

        # ret = Ptn.write(5, {'name': 'p5'})

        # Comp = api.odooenv['res.company']
        # cid = Comp.search_read([], ['name'])
        # print('comp ok,', cid,)

        # Ptn = api.odooenv['res.partner']
        # pid = Ptn.search_read([], ['name', 'company_id'])
        # print('ptn ok,', pid,)

        Ptn = api.odooenv['sale.order']
        pid = Ptn.search_read([], ['name', 'company_id', 'state', 'note'])
        print('ptn ok,', pid,)

        pid = Ptn.read([1], ['name', 'company_id', 'state', 'note'])
        print('ptn ok,', pid,)

        # Ptn.action_cancel(1)

        # Ptn.write(1, {'note': '123131'})

    def test_user_company(self):
        Comp = api.odooenv['res.company']
        User = api.odooenv['res.users']

        cid = Comp.search([('name', '=', 'Comp2')])[0]
        uid = User.search([('name', '=', 'user_12')])[0]
        print('comp ok,', cid, uid)

        user = User.read(uid, ['company_ids', 'company_id'])[0]
        print('comp ok,', cid, uid, user)

        values = {
            'company_ids': [cid] + user['company_ids'],
            'company_id': cid
        }
        user = User.write(uid, values)
        print('comp ok,', cid, uid, user)

    def test_create_company(self):
        Comp = api.odooenv['res.company']

        cid = Comp.create({'name': 'Comp2'})
        print('create ok,', cid)

    def test_create(self):
        obj = api.odooenv['res.users']
        name = 'user_12'
        values = {
            'login': name,
            'name': name,
            'email': name,
            'password': '123456',
        }

        uid = obj.create(values)
        group_id_account = api.env.ref('account.group_account_user')
        values = {
            ('in_group_%s' % group_id_account): True
        }
        print('create ok,', uid)
        res = obj.write(uid, values)
        print('write ok,', uid, res)

    def test_search(self):
        self.page.search([])

        print('sech ok,', self.page.store.dataList)

        # self.dataList

    # def test_fg(self):
    #     res = self.fields_get([], [])
    #     # print(res)

    #     for r, v in res.items():
    #         print(r, v['type'])
    #     pass


user = TestUser()
